package com.my.app.http;

import com.my.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 80002023 on 2016/6/7.
 */
public class BaseResult<T> {

    public int status;
    public List<T> records;

    public List<Error> errors;
//    public List

    public void put(T record) {
        if (records == null) {
            records = new ArrayList<T>();
        }
        records.add(record);
    }

    /**
     * @param args
     */
    public void newError(String... args) {
        if (args.length > 0) {
            if (errors == null) {
                errors = new ArrayList<Error>();
            }
            Error error = null;
            int code = -1;
            if (CommonUtils.isNumeric(args[0])) {
                code = Integer.valueOf(args[0]);
            }
            if (args.length == 1) {
                if (code > 0) {
                    error = new Error(code);
                } else {
                    error = new Error(args[0]);
                }
            } else if (args.length == 2) {
                error = new Error(code, args[1]);
            } else {
                error = new Error(code, args[1], args[2]);
            }
            errors.add(error);
        }
    }

    class Error {
        private int code;
        private String name;
        private String msg;

        public Error() {

        }

        public Error(int code) {
            this.code = code;
        }

        public Error(String msg) {
            this.msg = msg;
        }

        public Error(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Error(int code, String name, String msg) {
            this.code = code;
            this.name = name;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
